package com.galen.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

/**
 * CSRF演示使用配置
 */
@Configuration
public class CsrfConfig extends WebSecurityConfigurerAdapter {

    /**
     * 配置内存UserDetailsService
     *
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        UserDetails userDetails = User.withUsername("admin").password("{noop}123456").roles("admin").build();
        InMemoryUserDetailsManager userDetailsManager = new InMemoryUserDetailsManager(userDetails);
        auth.userDetailsService(userDetailsManager);
    }


    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().authenticated()
                .and().formLogin().defaultSuccessUrl("/helloWorld",true)
                .and().csrf().disable();
    }
}
